<template>
  <div id="detail">

    <detail-nav-bar class="detail-nav" @ShowNav="ShowNav" ref="dnb"/>
    <scroll class="content"
            ref="scroll"
            @scroll="Show_Scroll"
            :probe-type="3"
            :pull-up-load="true">
      <detail-swiper :topImages="topImages"/>
      <detail-base-info :GoodsInfo="goods"/>
      <detail-shop-info :ShopInfo="showInfo"/>
      <detail-goods-info :detailInfo="detailInfo" @imageLoad="imageLoad"/>
      <detail-param-info :GoodsParam="paramInfo" ref="detparam"/>
      <detail-comment-info :comment-info="commentInfo" ref="comment"/>
      <goods-list :goods="recommends"
                  ref="goods_list"/>
    </scroll>
    <detail-bottom-bar @addCart="addToCart"/>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
<!--    <toast ref="toast" :message="message" :isShow="show"/>-->
  </div>
</template>

<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import {getDetail, Goods, ShopInfo, GoodsParam, getRecommend} from "network/detail";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from "./childComps/detailBaseInfo";
  import DetailShopInfo from "./childComps/detailShopInfo";
  import Scroll from "components/common/scroll/Scroll";
  import DetailGoodsInfo from "./childComps/detailGoodsInfo";
  import DetailParamInfo from "./childComps/detailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import GoodsList from "components/content/goods/GoodsList";
  import {itemListenerMixin, backTopMixin} from "common/mixin";
  import DetailBottomBar from "./childComps/DetailBottomBar";
  import {BACK_POSITION} from "common/const.js";
  import {mapActions} from 'vuex'
  import Toast from "components/common/toast/Toast";

  export default {
    name: "Detail",
    components: {
      Toast,
      DetailBottomBar,
      GoodsList,
      DetailCommentInfo,
      DetailParamInfo,
      DetailGoodsInfo,
      DetailShopInfo,
      DetailBaseInfo,
      DetailSwiper,
      DetailNavBar,
      Scroll
    },
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        showInfo: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        PageHeight: null,
        recommends: [],
        currentIndex: 0,
        themeTopYs: [],
        message: '',
        show: false,
      }
    },
    mixins: [itemListenerMixin, backTopMixin],
    destroyed() {
      this.$bus.$off('itmImgLoad', this.itemImgListener)
    },
    created() {
      this.iid = this.$router.currentRoute.params.iid
      // 根据iid请求详情数据
      getDetail(this.iid).then(res => {
        // 获取顶部轮播图片
        const data = res.result
        // 展示数据
        const show = data.shopInfo
        // 获取参数信息
        const param = data.itemParams
        this.goods = new Goods(data.itemInfo, data.columns, show.services)
        this.topImages = data.itemInfo.topImages
        // 获取其他展示数据
        this.showInfo = new ShopInfo(show)
        // 创建店铺信息对象
        this.detailInfo = data.detailInfo;
        // 获取参数信息
        this.paramInfo = new GoodsParam(param.info, param.rule)
        // 获取评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }

        // this.$nextTick(()=>{
        //
        //
        // })

      }, err => {
        console.log(err);
      })
      getRecommend().then(res => {
        this.recommends = res.data.list
      }, err => {
        console.log(err);
      })
    },
    methods: {
      ...mapActions(['addCart']),
      imageLoad() {
        // 刷新scroll对象
        this.$refs.scroll.refresh()
        this.themeTopYs = []
        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.detparam.$el.offsetTop);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.goods_list.$el.offsetTop);
        console.log(this.themeTopYs);
      },
      ShowNav(index) {
        // console.log(index);
        // 点击商品数据显示位置
        this.PageHeight = -this.themeTopYs[index]
        // console.log(this.themeTopYs);
        this.$refs.scroll.scrollTo(0, this.PageHeight, 100)


      },
      Show_Scroll(position) {
        const positionY = -position.y
        // 图片滑动展示颜色(还有一种方法是将列表添加一个最大值，之后就不需要判断等于的情况了)
        for (let i = 0; i <= this.themeTopYs.length - 1; i++) {
          if (
            (this.currentIndex !== i &&
              positionY >= this.themeTopYs[i])
            &&
            (positionY < this.themeTopYs[i + 1] ||
              i === this.themeTopYs.length - 1)
          ) {
            this.currentIndex = i;
            this.$refs.dnb.$data.currentIndex = this.currentIndex;
          }
        }
        this.isShowBackTop = (positionY) > BACK_POSITION


      },
      addToCart() {
        const product = {}
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.iid = this.iid;
        product.price = this.goods.realPrice;
        this.addCart(product).then(res => {
          this.$toast.show(res)
        }, err => {
          console.log(err);
        })
      }
    },
    mounted() {

    }

  }
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 1002;
    background-color: #ffffff;
    height: 100vh;
  }

  .content {
    height: calc(100% - 44px - 58px);
  }

  .detail-nav {
    position: relative;
    z-index: 1003;
    background-color: #ffffff;
  }
</style>
